﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using Common;
using Common.Config;
using Common.Model;
using ConsoleService.API;
using ConsoleService.Cache;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Config;
using NLog.Web;
using Quartz;

namespace ConsoleService.Job
{
    /// <summary>
    /// 创建IJob的实现类，并实现Excute方法
    /// </summary>
    public class MyJob : IJob
    {
        private Logger logger = LogManager.GetCurrentClassLogger();
        public MyJob()
        {
            NLogBuilder.ConfigureNLog("nlog.config");
        }

        public Task Execute(IJobExecutionContext context)
        {
            var triggerData = context.Trigger.JobDataMap;//获取Trigger中的参数
            var data = context.MergedJobDataMap;//获取Job和Trigger中合并的参数
            var value1 = triggerData.Get("key1");
            var value2 = triggerData.GetString("key2");

            return Task.Run(() =>
            {
                //Dictionary<string, string> parameters = new Dictionary<string, string>();
                //parameters.Add("Account", "admin");
                //parameters.Add("PassWord", "123456");
                //RequestModel request = new RequestModel()
                //{
                //    Method = RestSharp.Method.POST,
                //    Parameters = parameters,
                //    Resources = "/api/User/Login",
                //    WebSide = "http://59.110.163.255:8080/",
                //};
                //var rsult = WebHelper.HttpRequest<Login>(request);

                //var url = $"http://59.110.163.255:8080/api/User/WrittenQueue?Message=任务回调执行：{DateTime.Now.ToString()}";
                //var rsult1 = WebHelper.HttpRequest(url, rsult.data);
                //logger.Warn("任务调度执行：" + DateTime.Now.ToString() + rsult1);
                //Console.WriteLine("任务调度执行：" + DateTime.Now.ToString() + rsult1);

                Stopwatch s = new Stopwatch();
                s.Start();
                for (int i = 0; i < 50; i++)
                {
                    var rsult2 = WebHelper.HttpRequest($"{Configs.WebUrl}api/User/GetCacheLoginLog");
                }
                s.Stop();
                logger.Warn($"任务调度执行A：{ s.Elapsed.ToString()}" + DateTime.Now.ToString());
            });
        }
    }
}
